import { useState } from 'react'
import { BrowserRouter,useRoutes } from 'react-router-dom'
import React from 'react'
import routes from '~react-pages'
import './App.css'

function App() {
  const Routes = () =>{
    //设置全局跳转路由函数
    React.navigate = useNavigate()
    //设置全局获取参数
    let [searchParams] = useSearchParams()

    React.params = (name:string) => searchParams.get(name ?? '')
     //location.pathname
     const {pathname} = useLocation()

     //路由守卫 判断登录
     if(!React.RouterRules.includes(pathname)) check()
    
    //返回自动生成的路由列表
    return useRoutes(routes);
  }
  const check = async ()=>{
    //封装数据
    let id = React.business.id ? React.business.id : 0;
    
    let mobile = React.business.mobile ? React.business.mobile : '';   

    var result = await React.request.post('/business/check',{id,mobile})
    if(result.code==0){
      //删除cookie中的值
      React.business = {};
      React.cookie.remove('business')
      React.toast(result.msg,result.url,'fail');
      return false
    }
    React.business = result.data;

    return false
  }
  // //页面加载中loading...
  const fallback = ()=>(<React.UI.AutoCenter><React.UI.DotLoading color='primary' /></React.UI.AutoCenter>)
  // 当内容正在加载时显示后备方案 Suspense 
  return (
    <>
       <BrowserRouter future={{v7_startTransition: true, v7_relativeSplatPath: true}}>
          <React.Suspense fallback={fallback()}>
            <Routes />
          </React.Suspense>
       </BrowserRouter>
    </>
  )
}

export default App
